package com.pangu.ui;

import com.pangu.AMApplication;
import com.pangu.AMHandler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 父类Activity，共享客户端环境。
 * <p/>
 * Created by cxlin
 */
public abstract class AActivity<A extends AMApplication> extends AppCompatActivity {

    public static final String TAG = AActivity.class.getSimpleName();

    // 记录所有打开的activity
    private static Vector<Activity> activityList = new Vector<Activity>();

    // 整个应用共享的环境
    protected A imContext;

    // 处理器
    private MMHandler mmHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 记录
        activityList.add(this);

        imContext = (A) getApplication();
        // 注册处理器
        mmHandler = new MMHandler(this);
        imContext.registerSubHandler(mmHandler);
        imContext.setMWindowToken(this);
    }

    @Override
    protected void onResume() {
        imContext.setMWindowToken(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        imContext.setMWindowToken(null);
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        // 移除记录
        activityList.remove(this);

        // 取消注册其处理
        if (mmHandler != null) {
            imContext.unregisterSubHandler(mmHandler);
        }

        super.onDestroy();
    }

    /**
     * 关闭所有视图
     */
    protected void closeAllActivity() {
        // 关闭界面，确保旧数据完全删除，同时不受外界影响
        List<Activity> oldActivity = new ArrayList<Activity>(activityList);
        activityList.clear();
        for (Activity activity : oldActivity) {
            activity.finish();
        }
    }

    /**
     * 发送消息
     *
     * @param message
     */
    protected void sendMessage(Message message) {
        mmHandler.sendMessage(message);
    }

    /**
     * 发送延时消息
     *
     * @param message
     * @param delay
     */
    protected void sendDelayMessage(Message message, long delay) {
        mmHandler.sendMessageDelayed(message, delay);
    }

    /**
     * 发送空消息
     *
     * @param what
     */
    protected void sendEmptyMessage(int what) {
        mmHandler.sendEmptyMessage(what);
    }

    /**
     * 发送延时空消息
     *
     * @param what
     * @param delay
     */
    protected void sendEmptyMessage(int what, long delay) {
        removeEmptyMessage(what);
        mmHandler.sendEmptyMessageDelayed(what, delay);
    }

    /**
     * 移除某个消息
     *
     * @param what
     */
    protected void removeEmptyMessage(int what) {
        mmHandler.removeMessages(what);
    }

    /**
     * 子类处理对应的消息
     *
     * @param msg
     */
    public void subHandleMessage(Message msg) {

    }

    /**
     * 主界面消息处理器，希望它存在的时候处理所有可能的消息
     */
    private static class MMHandler extends AMHandler {

        private final WeakReference<AActivity> mActivityRef;

        public MMHandler(AActivity context) {
            super();

            mActivityRef = new WeakReference<AActivity>(context);
        }

        @Override
        public void handleMessage(Message msg) {
            AActivity aActivity = mActivityRef.get();
            if (aActivity == null) {
                return;
            }

            // 如果已经调用了finish了。则不处理
            if (aActivity.isFinishing()) {
                return;
            }
            switch (msg.what) {
                default:
                    aActivity.subHandleMessage(msg);
                    break;
            }
        }
    }

}
